package BOH.GUI;
import BOH.data.EateryDBConnection;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Havemman
 * CSC 289 0B01
 * February 13, 2014
 */

public class AddEditRemoveEmployeePanel extends JPanel
{
    // Variables
    private JFrame frame;
    private JPanel mainPanel;       // Holds the mainPanel
    private JPanel tempPanel;       // Holds the tempPanel
    private JPanel westPanel;       // Holds the westPanel
    private JPanel centerPanel;     // Holds the centerPanel
    private JPanel eastPanel;       // Holds the eastPanel
    private JPanel buttonPanel;     // Holds the button
    private String[] employeeInformationArray; // Holds selected employee information
    private String editForm;        // Holds the add/edit option
    private String sqlStatement;    // Adds/Updates employee information
    private String sqlStatement2;   // Selects the last employeeID
    private String employeeID;      // Holds the next employeeID
    
    // Holds the JLabels to enter employee information
    private JLabel employeeIDLabel, lastNameLabel, firstNameLabel, ssnLabel, dobLabel, addressLabel,
            cityLabel, stateLabel, zipLabel, telephoneLabel, hireDateLabel, hourlyWageLabel, filingStatusLabel, 
            dependentsLabel, jobCodeLabel;
    
    // Holds the JTextFields to enter employee information
    private JTextField employeeIDTextField, lastNameTextField, firstNameTextField,
            ssnTextField, dobTextField, addressTextField, cityTextField, stateTextField, 
            zipTextField, telephoneTextField, hireDateTextField, hourlyWageTextField, filingStatusTextField,
            dependentsTextField, jobCodeTextField;
    
    // Holds the button information.
    private JButton addUpdateButton;
    
    /*
     * Constructor
     */
    
    public AddEditRemoveEmployeePanel()
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("Employee Information");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Add main panel
        this.buildMainPanel();
        frame.add(mainPanel);
        this.fillTextFields();
        
        // Add content to the window.
        frame.setSize(400, 350);
        frame.setResizable(false);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);  
    }
    
    /*
     * Edit Screen Constructor
     */
    
    public AddEditRemoveEmployeePanel(String[] array, String string)
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("Employee Information");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Get employee information
        employeeInformationArray = array;
        editForm = string;
        
        // Add main panel
        this.buildMainPanel();
        frame.add(mainPanel);
        this.fillTextFields();
        
        // Add content to the window.
        frame.setSize(400, 350);
        frame.setResizable(false);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);  
    }
    
    /**
     * The buildMainPanel builds the 
     * mainPanel.
     */
    
    private void buildMainPanel() 
    {
        // Create mainPanel
        this.mainPanel = new JPanel(new BorderLayout());
        this.mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create tempPanel for formatting
        this.tempPanel = new JPanel(new GridLayout(1, 0));
        this.tempPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create buttonPanel
        buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the addUpdateButton
        addUpdateButton = new JButton("Add Employee");
        addUpdateButton.addActionListener(new addEditEmployeeButtonListener());
        buttonPanel.add(addUpdateButton);
        
        // Get Panels
        westPanel = buildWestPanel();
        centerPanel = buildCenterPanel();
        eastPanel = buildEastPanel();
        
        // Add panels
        tempPanel.add(westPanel, BorderLayout.WEST);
        tempPanel.add(centerPanel, BorderLayout.CENTER);
        tempPanel.add(eastPanel,BorderLayout.EAST);
        mainPanel.add(tempPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }
    
    /**
     * The buildWestPanel builds the 
     * westPanel.
     * @return
     */
    
    private JPanel buildWestPanel() 
    {
        // Create westPanel
        westPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the JLabels
        employeeIDLabel = new JLabel("Employee ID: ");
        ssnLabel = new JLabel("SSN: ");
        cityLabel = new JLabel("City: ");
        telephoneLabel = new JLabel("Telephone: ");
        filingStatusLabel = new JLabel("Filing Status: (Ex: D, M, S)");
        
        // Create employeeIDTextField
        employeeIDTextField = new JTextField(10);
        employeeIDTextField.setHorizontalAlignment(JTextField.CENTER);
        employeeIDTextField.setEditable(false); // Sets textfield to read-only
        employeeIDTextField.setToolTipText("Displays employee ID:");
        
        // Create ssnTextField
        ssnTextField = new JTextField(10);
        ssnTextField.setToolTipText("Displays employee SSN");
        
        // Create cityTextField
        cityTextField = new JTextField(10);
        cityTextField.setToolTipText("Displays employee city");
        
        // Create telephoneTextField
        telephoneTextField = new JTextField(10);
        telephoneTextField.setToolTipText("Displays employee telephone");
        
        // Create filingStatusTextField
        filingStatusTextField = new JTextField(10);
        filingStatusTextField.setToolTipText("Displays employee filing status");
        
        // Add JLabels and JTextFields to westPanel
        westPanel.add(employeeIDLabel);
        westPanel.add(employeeIDTextField);
        westPanel.add(ssnLabel);
        westPanel.add(ssnTextField);
        westPanel.add(cityLabel);
        westPanel.add(cityTextField);
        westPanel.add(telephoneLabel);
        westPanel.add(telephoneTextField);
        westPanel.add(filingStatusLabel);
        westPanel.add(filingStatusTextField);
        
        // Return
        return westPanel;
    }
    
    /**
     * The buildCenterPanel builds the 
     * centerPanel.
     * @return 
     */
    
    private JPanel buildCenterPanel() 
    {
        // Create centerPanel
        centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the JLabels
        lastNameLabel = new JLabel("Last Name: ");
        dobLabel = new JLabel("DOB: (Ex. 2014/03/30)");
        stateLabel = new JLabel("State: (Ex. NC)");
        hireDateLabel = new JLabel("Hire Date: (Ex. 2014/03/30)");
        dependentsLabel = new JLabel("Dependents: ");
        
        // Create lastNameTextField
        lastNameTextField = new JTextField(10);
        lastNameTextField.setToolTipText("Displays employee last name");
        
        // Create dobTextField
        dobTextField = new JTextField(10);
        dobTextField.setToolTipText("Displays employee DOB");
        
        // Create stateTextField
        stateTextField = new JTextField(10);
        stateTextField.setToolTipText("Displays employee state residence");
        
        // Create hireDateTextField
        hireDateTextField = new JTextField(10);
        hireDateTextField.setToolTipText("Displays employee hire date");
        
        // Create dependentsTextField
        dependentsTextField = new JTextField(10);
        dependentsTextField.setToolTipText("Displays employee dependents");
       
        // Add components to centerPanel
        centerPanel.add(lastNameLabel);
        centerPanel.add(lastNameTextField);
        centerPanel.add(dobLabel);
        centerPanel.add(dobTextField);
        centerPanel.add(stateLabel);
        centerPanel.add(stateTextField);
        centerPanel.add(hireDateLabel);
        centerPanel.add(hireDateTextField);
        centerPanel.add(dependentsLabel);
        centerPanel.add(dependentsTextField);
        
        // Return
        return centerPanel;
    }
    
    /**
     * The buildEastPanel builds the 
     * eastPanel.
     * @return 
     */
    
    private JPanel buildEastPanel() 
    {
        // Create eastPanel
        eastPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        eastPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the JLabels
        firstNameLabel = new JLabel("First Name: ");
        addressLabel = new JLabel("Address: ");
        zipLabel = new JLabel("Zip: (Ex. 28303)");
        hourlyWageLabel = new JLabel("Hourly Wage: ");
        jobCodeLabel = new JLabel("Job Code: (Ex. M, C, or K");
        
        // Create firstNameTextField
        firstNameTextField = new JTextField(10);
        firstNameTextField.setToolTipText("Displays employee first name");
        
        // Create addressTextField
        addressTextField = new JTextField(10);
        addressTextField.setToolTipText("Displays employee address");
        
        // Create zipTextField
        zipTextField = new JTextField(10);
        zipTextField.setToolTipText("Displays employee zip");
        
        // Create hourlyWageTextField
        hourlyWageTextField = new JTextField(10);
        hourlyWageTextField.setToolTipText("Displays employee hourly wage");
        
        // Create jobCodeTextField
        jobCodeTextField = new JTextField(10);
        jobCodeTextField.setToolTipText("Displays employee job code");
        
        // Add components to eastPanel
        eastPanel.add(firstNameLabel);
        eastPanel.add(firstNameTextField);
        eastPanel.add(addressLabel);
        eastPanel.add(addressTextField);
        eastPanel.add(zipLabel);
        eastPanel.add(zipTextField);
        eastPanel.add(hourlyWageLabel);
        eastPanel.add(hourlyWageTextField);
        eastPanel.add(jobCodeLabel);
        eastPanel.add(jobCodeTextField);
        
        // Return
        return eastPanel;
    }
    
    /**
     * The fillTextFields method gets the employee
     * to fill the TextFields for editing.
     * @param e 
     */
    
    private void fillTextFields()
    {
        // Determines which version of form to open
        switch (editForm) 
        {
            case "Edit":
                // Fill TextFields with employee information
                employeeIDTextField.setText(employeeInformationArray[0]);
                lastNameTextField.setText(employeeInformationArray[1]);
                firstNameTextField.setText(employeeInformationArray[2]);
                ssnTextField.setText(employeeInformationArray[3]);
                dobTextField.setText(employeeInformationArray[4]);
                addressTextField.setText(employeeInformationArray[5]);
                cityTextField.setText(employeeInformationArray[6]);
                stateTextField.setText(employeeInformationArray[7]);
                zipTextField.setText(employeeInformationArray[8]);
                telephoneTextField.setText(employeeInformationArray[9]);
                hireDateTextField.setText(employeeInformationArray[10]);
                hourlyWageTextField.setText(employeeInformationArray[11]);
                filingStatusTextField.setText(employeeInformationArray[12]);
                dependentsTextField.setText(employeeInformationArray[13]);
                jobCodeTextField.setText(employeeInformationArray[14]);
                addUpdateButton.setText("Edit Employee");
                break;
            case "Add":
                // Fill employeeID Textfield with
                // the next employeeID number available
                employeeID = getMaxEmployeeID();
                employeeIDTextField.setText(employeeID);
                break;
            case "Remove":
                // Fill TextFields with employee information
                employeeIDTextField.setText(employeeInformationArray[0]);
                lastNameTextField.setText(employeeInformationArray[1]);
                firstNameTextField.setText(employeeInformationArray[2]);
                ssnTextField.setText(employeeInformationArray[3]);
                dobTextField.setText(employeeInformationArray[4]);
                addressTextField.setText(employeeInformationArray[5]);
                cityTextField.setText(employeeInformationArray[6]);
                stateTextField.setText(employeeInformationArray[7]);
                zipTextField.setText(employeeInformationArray[8]);
                telephoneTextField.setText(employeeInformationArray[9]);
                hireDateTextField.setText(employeeInformationArray[10]);
                hourlyWageTextField.setText(employeeInformationArray[11]);
                filingStatusTextField.setText(employeeInformationArray[12]);
                dependentsTextField.setText(employeeInformationArray[13]);
                jobCodeTextField.setText(employeeInformationArray[14]);
                addUpdateButton.setText("Remove Employee");
                break;
        }
    }
    
    /**
     * The getMaxEmployeeID method returns
     * the value in the comboBox
     * @param e 
     */
    
    public String getMaxEmployeeID()
    {
        // SQL statement for max employeeID
        sqlStatement2 = "SELECT MAX(employeeID) " +
                        "FROM Employee";
        
        // Get the max number
        String ID = EateryDBConnection.getDataString(sqlStatement2);
        
        // Convert number in order to increment it
        int number = Integer.parseInt(ID);
        number++;
        
        employeeID = Integer.toString(number);
            
        // Return
        return employeeID;
    }
    
    
    /**
     * The addUpdateEmployee method adds/updates 
     * the employee in the database.
     * @param e 
     */
    
    public String addUpdateEmployee()
    {
        // Determine which action must take place
        switch (editForm) 
        {
            case "Edit":
                sqlStatement = "UPDATE Employee " +
                               "SET LastName = '" + lastNameTextField.getText() + "' ," +
                               "FirstName = '" + firstNameTextField.getText() + "', " +
                               "SSN = '" + ssnTextField.getText() + "', " +
                               "DOB = '" + dobTextField.getText() + "', " +
                               "Address = '" + addressTextField.getText() + "', " +
                               "City = '" + cityTextField.getText() + "', " +
                               "State = '" + stateTextField.getText() + "', " +
                               "Zip = '" + zipTextField.getText() + "', " +
                               "Telephone = '" + telephoneTextField.getText() + "', " +
                               "HireDate = '" + hireDateTextField.getText() + "', " +
                               "HourlyWage = " + hourlyWageTextField.getText() + ", " +
                               "FilingStatus = '" + filingStatusTextField.getText() + "', " +
                               "Dependents = " + dependentsTextField.getText() + ", " +
                               "JobCode = '" + jobCodeTextField.getText() + "' " +
                               "WHERE EmployeeID = " + employeeIDTextField.getText();
                break;
            case "Add":
                sqlStatement = "INSERT INTO Employee (LastName, " +
                             "FirstName, SSN, DOB, Address, " +
                             "City, State, Zip, Telephone, " +
                             "HireDate, HourlyWage, FilingStatus," +
                             "Dependents, JobCode) " + 
                             "VALUES ('" +
                             lastNameTextField.getText() +  "', " + "'" +
                             firstNameTextField.getText() + "', " + "'" +
                             ssnTextField.getText() + "', " + "'" +
                             dobTextField.getText() + "', "+ "'" +
                             addressTextField.getText() + "', " + "'" +
                             cityTextField.getText() + "', " + "'" +
                             stateTextField.getText() + "', " + "'" +
                             zipTextField.getText() + "', " + "'" +
                             telephoneTextField.getText() + "', " + "'" + 
                             hireDateTextField.getText() + "', " + 
                             hourlyWageTextField.getText() + ", " + "'" +
                             filingStatusTextField.getText() + "', " + 
                             dependentsTextField.getText() + ", " + "'" +
                             jobCodeTextField.getText() + "'" + ")";
                break;
            case "Remove":
                sqlStatement = "DELETE FROM Employee " +
                               "WHERE EmployeeID = " 
                               + employeeIDTextField.getText();
                             
                break;
        }
        
        // Return sqlStatement
        return sqlStatement;
    }
    
    /*
     * The addEditEmployeeButtonListener is used to add 
     * or edit an employee.
     */
    
    private class addEditEmployeeButtonListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Send information to the database
            switch (editForm) 
            {
                case "Edit":
                    // Update employee information
                    sqlStatement = addUpdateEmployee();
                    EateryDBConnection.updateDatabase(sqlStatement);
                    JOptionPane.showMessageDialog(null, "Employee Information Updated");
                    frame.setVisible(false);
                    break;
                case "Add":
                    // Add employee information
                    sqlStatement = addUpdateEmployee();
                    EateryDBConnection.updateDatabase(sqlStatement);
                    JOptionPane.showMessageDialog(null, "Employee Added");
                    frame.setVisible(false);
                    break;
                case "Remove":
                    // Remove employee information
                    sqlStatement = addUpdateEmployee();
                    EateryDBConnection.updateDatabase(sqlStatement);
                    JOptionPane.showMessageDialog(null, "Employee Removed");
                    frame.setVisible(false);
                    break;
            }
        }
    }
}
